﻿using Company.Core.Interfaces;
using Company.Database;
using Company.Database.Sqlite;
using Company.Plc;
using Company.Plc.NModbus;
using Company.Shell.Dialogs;
using Company.Shell.Interfaces;
using Company.Shell.Services;
using Company.Shell.Styles;
using Company.Shell.ViewModels;
using Company.Shell.Views;
using Microsoft.Extensions.DependencyInjection;

namespace Company.Shell.Dependencylnjection
{
    /// <summary>
    /// 所有的页面服务都放到Services容器当中，依赖注入
    /// </summary>
    public class ServiceManager
    {
        public static IServiceProvider Load()
        {
           var services = new ServiceCollection();


            //注册数据库
            services.AddSingleton<IUserRepository, UserRepository>();//把IUserRepository和UserRepository放入服务容器
            services.AddTransient<IAlarmRepository, AlarmRepository>();//报警数据需要频繁写入且要求高可靠性。因此使用瞬时服务



            // 批量注册所有 View 和 ViewModel
            services.Scan(scan => scan
                .FromAssemblyOf<ServiceManager>()
                .AddClasses(classes => classes.Where(t => t.Name.EndsWith("View") || t.Name.EndsWith("ViewModel")))
                .AsSelf()
                .WithSingletonLifetime());

           
            //注册业务层
            services.AddSingleton<IConfigProvider, ConfigProvider>();

            services.AddSingleton<CurrentAlarmViewModel>();
            services.AddSingleton<CurrentAlarmView>();
            services.AddSingleton<HistoryAlarmViewmodel>();
            services.AddSingleton<HistoryAlarmView>();


            services.AddSingleton<ISession, Session>();
            services.AddSingleton<IPlc, NModbusPlc>();
            services.AddSingleton<IDialogService, DialogService>();


            services.AddSingleton<IRoomManager, RoomManager>();
            services.AddSingleton<ICollectManager, CollectManager>();
            services.AddSingleton<ICacheManager, CacheManager>();
            services.AddSingleton<IHardwareManager, HardwareManager>();
            services.AddSingleton<IMouseManager, MouseManager>();
            services.AddSingleton<ProgramManager>();
            services.AddSingleton<IProgramManager>(sp => sp.GetRequiredService<ProgramManager>());
            services.AddSingleton<IConfigManager, ConfigManager>();

            return services.BuildServiceProvider();
        }
    }
}
